package com.wealth.system.web.listener;

import java.util.List;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.alibaba.fastjson.JSONArray;
import com.wealth.platform.service.ISldConfigService;
import com.wealth.platform.vo.SldConfigVO;

import iaas.redis.RedisService;

/**
 * @author chan
 * 
 * 启动项目，将企业二级域名配置列表加载到Redis中
 * 
 * changelog
 * -------------------------
 * create  by chan on 2016-03-29 
 * modify by chan on 2016-09-13 
 * JSON解析替换  net.sf.json -> com.alibaba.fastjson
 * Redis存储数据优化 SldConfig  -> SLDConfigVO 
 * 
 */
public class StartUpListener implements ApplicationListener<ContextRefreshedEvent> {

	private RedisService redisService;
	private ISldConfigService sldConfigService;
	
	public RedisService getRedisService() {
		return redisService;
	}

	public void setRedisService(RedisService redisService) {
		this.redisService = redisService;
	}

	public ISldConfigService getSldConfigService() {
		return sldConfigService;
	}

	public void setSldConfigService(ISldConfigService sldConfigService) {
		this.sldConfigService = sldConfigService;
	}

	/* ---------------- getter/setter ----------------*/
	/**
	 * 在spring加载完成后执行
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		try {
			if (event.getApplicationContext().getParent() == null) {
				//加載企业二级域名列表
				List<SldConfigVO> sldConfgList = sldConfigService.getSldConfigVoList();
				String config = JSONArray.toJSONString(sldConfgList);
				redisService.set("redis_sld_config", config);
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


